//
//  SoundCodeSettingViewController.m
//  SIPphone
//
//  Created by wrt on 14-2-26.
//  Copyright (c) 2014年 wrtsoft. All rights reserved.
//

#import "SoundCodeSettingViewController.h"
#import "CodeSettingCell.h"

@interface SoundCodeSettingViewController ()

@end

@implementation SoundCodeSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"audio codec", @"音频编解码 Audio codec");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.audioCodeArray = [[NSMutableArray alloc] init];
    self.codeNumberArray = [[NSMutableArray alloc] init];
    
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    self.audioCodeCount = [interface getAudioCodeCount];
    for (int i = 0; i < self.audioCodeCount; i++)
    {
        self.audioCodeNumber = [interface getAudioCodeNumber:i];
        NSString *tempAudioCodeName = [interface getAudioAndVideoName:self.audioCodeNumber];
        [self.audioCodeArray addObject:tempAudioCodeName];
        [self.codeNumberArray addObject:[NSNumber numberWithUnsignedLong:self.audioCodeNumber]];
    }
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath:@"audioCode.plist"]])
    {
        self.audioCodeDic = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath:@"audioCode.plist"]];
    }
    else
    {
        NSMutableArray *tempYesOrNoArray = [[NSMutableArray alloc] init];
        for (int i = 0 ; i < self.audioCodeCount; i++)
        {
            [tempYesOrNoArray addObject:[NSNumber numberWithInt:1]];
        }
        
        self.audioCodeDic = [[NSMutableDictionary alloc] initWithObjects:tempYesOrNoArray forKeys:self.audioCodeArray];
        
        [self.audioCodeDic writeToFile:[self dataFilePath:@"audioCode.plist"] atomically:YES];
    }
    
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath:@"audioOrder.plist"]])
    {
        [self.audioCodeArray writeToFile:[self dataFilePath:@"audioOrder.plist"] atomically:YES];
    }
    
    //编辑按钮
    self.editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"editor", @"编辑 Editor") style:UIBarButtonItemStyleBordered target:self action:@selector(editButtonPressed)];
    self.navigationItem.rightBarButtonItem = self.editButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.audioCodeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CodeSettingCell *cell;
    static NSString *cellIdentifier = @"CodeSettingCell";
    
    cell = (CodeSettingCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //判断是否获取到复用cell,没有则从xib中初始化一个cell
    if (!cell)
    {
        //将cell.xib中的所有对象载入
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CodeSettingCell" owner:nil options:nil];
        //第一个对象就是Cell了
        cell = [nib objectAtIndex:0];
    }
    
    NSNumber *onOroffNumber = [self.audioCodeDic objectForKey:self.audioCodeArray[indexPath.row]];
    if ([onOroffNumber intValue] == 1)
    {
        [cell.codeSwitch setOn:YES];
    }
    else if ([onOroffNumber intValue] == 0)
    {
        [cell.codeSwitch setOn:NO];
    }
    
    cell.codeTextLabel.text = self.audioCodeArray[indexPath.row];
    cell.codetType = @"audio";
    cell.codeNumber = [self.codeNumberArray[indexPath.row] unsignedLongValue];
    cell.tag = indexPath.row + 1;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //    需要的移动行
    NSInteger fromRow = [sourceIndexPath row];
    //    获取移动某处的位置
    NSInteger toRow = [destinationIndexPath row];
    
    
    NSInteger tempDir = fromRow - toRow;
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    
    
    [interface setAudioAndVideoMove:[self.codeNumberArray[fromRow] unsignedLongValue] andDir:(int)tempDir];
    
    
    //    从数组中读取需要移动行的数据
    id object = [self.audioCodeArray objectAtIndex:fromRow];
    //    在数组中移动需要移动的行的数据
    [self.audioCodeArray removeObjectAtIndex:fromRow];
    //    把需要移动的单元格数据在数组中，移动到想要移动的数据前面
    [self.audioCodeArray insertObject:object atIndex:toRow];
    
    id objectForNumber = [self.codeNumberArray objectAtIndex:fromRow];
    //    在数组中移动需要移动的行的数据
    [self.codeNumberArray removeObjectAtIndex:fromRow];
    //    把需要移动的单元格数据在数组中，移动到想要移动的数据前面
    [self.codeNumberArray insertObject:objectForNumber atIndex:toRow];
    
    //写入属性文件中
    [self.audioCodeArray writeToFile:[self dataFilePath:@"audioOrder.plist"] atomically:YES];
}

- (void)editButtonPressed
{
    
    //进入编辑模式
    [self.tableView setEditing:!self.tableView.editing];
    if (self.tableView.editing)
    {
        [self.editButton setTitle:NSLocalizedString(@"complete", @"完成 Complete")];
    }
    else
    {
        [self.editButton setTitle:NSLocalizedString(@"editor", @"编辑 Editor")];
    }
}

//沙盒文件路径 audioCode.plist 和 audioOrder.plist
- (NSString *)dataFilePath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:fileName];
}
@end
